using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACKETPARSERMAX20.CORE;

namespace PACKETPARSERMAX20.CORE.DEFINITIONS
{
    public class SMSG_ITEM_QUERY_SINGLE_RESPONSE_DEF : DefinitionBase
    {
        public override bool Process()
        {
            if (AvailableBytes <= 4)
            {
                base.Warning = "CORRUPT PACKET.";
                return false;
            }

            item_template template = GetItemTemplate();
            GlobalCache.AddItemTemplateSum(template, ClientBuild);
            return Validate();

        }

        public item_template GetItemTemplate()
        {
            ResetPosition();

            item_template template = new item_template();

            template.entry = ReadUInt32();

            template.class_ = (byte)ReadUInt32();
            template.subclass = (byte)ReadUInt32();
            template.unk0 = (int)ReadUInt32();                                 // new 2.0.3, not exist in wdb cache?
            template.name = ReadString();
            ReadString(); //Name2
            ReadString(); //Name3
            ReadString(); //Name4
            template.displayid = ReadUInt32();
            template.quality = (byte)ReadUInt32();
            template.flags = ReadUInt32();

            if (ClientBuild >= 10314)
            {
                template.flags2 = ReadUInt32();
            }

            template.buyprice = ReadUInt32();
            template.sellprice = ReadUInt32();
            template.inventorytype = (byte)ReadUInt32();
            template.allowableclass = (byte)ReadUInt32();

            template.allowablerace = (int)ReadUInt32();
            template.itemlevel = (ushort)ReadUInt32();
            template.requiredlevel = (byte)ReadUInt32();
            template.requiredskill = (ushort)ReadUInt32();
            template.requiredskillrank = (ushort)ReadUInt32();
            template.requiredspell = ReadUInt32();
            template.requiredhonorrank = ReadUInt32();
            template.requiredcityrank = ReadUInt32();
            template.requiredreputationfaction = (ushort)ReadUInt32();
            template.requiredreputationrank = (ushort)ReadUInt32();
            template.maxcount = (short)ReadUInt32();
            template.stackable = (short)ReadUInt32();
            template.containerslots = (byte)ReadUInt32();

            if (ClientBuild >= 9183)
            {
                template.statscount = (byte)ReadUInt32();
            }
            else
            {
                template.statscount = 10;
            }

            for (int i = 1; i <= template.statscount; i++)
            {

                switch (i)
                {
                    case 1:
                        {
                            template.stat_type1 = (byte)ReadUInt32();
                            template.stat_value1 = (short)ReadUInt32();
                        } break;
                    case 2:
                        {
                            template.stat_type2 = (byte)ReadUInt32();
                            template.stat_value2 = (short)ReadUInt32();
                        } break;
                    case 3:
                        {
                            template.stat_type3 = (byte)ReadUInt32();
                            template.stat_value3 = (short)ReadUInt32();
                        } break;
                    case 4:
                        {
                            template.stat_type4 = (byte)ReadUInt32();
                            template.stat_value4 = (short)ReadUInt32();
                        } break;
                    case 5:
                        {
                            template.stat_type5 = (byte)ReadUInt32();
                            template.stat_value5 = (short)ReadUInt32();
                        } break;
                    case 6:
                        {
                            template.stat_type6 = (byte)ReadUInt32();
                            template.stat_value6 = (short)ReadUInt32();
                        } break;
                    case 7:
                        {
                            template.stat_type7 = (byte)ReadUInt32();
                            template.stat_value7 = (short)ReadUInt32();
                        } break;
                    case 8:
                        {
                            template.stat_type8 = (byte)ReadUInt32();
                            template.stat_value8 = (short)ReadUInt32();
                        } break;
                    case 9:
                        {
                            template.stat_type9 = (byte)ReadUInt32();
                            template.stat_value9 = (short)ReadUInt32();
                        } break;
                    case 10:
                        {
                            template.stat_type10 = (byte)ReadUInt32();
                            template.stat_value10 = (short)ReadUInt32();
                        } break;
                }
            }

            if (ClientBuild >= 9183)
            {
                template.scalingstatdistribution = (short)ReadUInt32();            // scaling stats distribution
                template.scalingstatvalue = ReadUInt32();                   // some kind of flags used to determine stat values column
            }

            int DamageCount = 0;

            if (ClientBuild > 9551)
            {
                DamageCount = 2;
            }
            else
            {
                DamageCount = 5;
            }

            for (int i = 1; i <= DamageCount; i++)
            {

                switch (i)
                {
                    case 1:
                        {
                            template.dmg_min1 = ReadSingle();
                            template.dmg_max1 = ReadSingle();
                            template.dmg_type1 = (byte)ReadUInt32();
                        } break;
                    case 2:
                        {
                            template.dmg_min2 = ReadSingle();
                            template.dmg_max2 = ReadSingle();
                            template.dmg_type2 = (byte)ReadUInt32();
                        } break;
                    default:
                        {
                            ReadSingle(); //dmg_min2 3-5
                            ReadSingle(); //dmg_max2 3-5
                            ReadUInt32(); //dmg_type2 3-5
                        } break;
                }

            }

            // resistances (7)
            template.armor = (ushort)ReadUInt32();
            template.holy_res = (byte)ReadUInt32();
            template.fire_res = (byte)ReadUInt32();
            template.nature_res = (byte)ReadUInt32();
            template.frost_res = (byte)ReadUInt32();
            template.shadow_res = (byte)ReadUInt32();
            template.arcane_res = (byte)ReadUInt32();

            template.delay = (ushort)ReadUInt32();
            template.ammo_type = (byte)ReadUInt32();
            template.rangedmodrange = ReadSingle();

            template.spellid_1 = ReadUInt32();
            template.spelltrigger_1 = (byte)ReadUInt32();
            template.spellcharges_1 = (short)ReadInt32();
            template.spellcooldown_1 = ReadInt32();
            template.spellcategory_1 = (ushort)ReadUInt32();
            template.spellcategorycooldown_1 = ReadInt32();

            template.spellid_2 = ReadUInt32();
            template.spelltrigger_2 = (byte)ReadUInt32();
            template.spellcharges_2 = (short)ReadInt32();
            template.spellcooldown_2 = ReadInt32();
            template.spellcategory_2 = (ushort)ReadUInt32();
            template.spellcategorycooldown_2 = ReadInt32();

            template.spellid_3 = ReadUInt32();
            template.spelltrigger_3 = (byte)ReadUInt32();
            template.spellcharges_3 = (short)ReadInt32();
            template.spellcooldown_3 = ReadInt32();
            template.spellcategory_3 = (ushort)ReadUInt32();
            template.spellcategorycooldown_3 = ReadInt32();

            template.spellid_4 = ReadUInt32();
            template.spelltrigger_4 = (byte)ReadUInt32();
            template.spellcharges_4 = (short)ReadInt32();
            template.spellcooldown_4 = ReadInt32();
            template.spellcategory_4 = (ushort)ReadUInt32();
            template.spellcategorycooldown_4 = ReadInt32();

            template.spellid_5 = ReadUInt32();
            template.spelltrigger_5 = (byte)ReadUInt32();
            template.spellcharges_5 = (short)ReadInt32();
            template.spellcooldown_5 = ReadInt32();
            template.spellcategory_5 = (ushort)ReadUInt32();
            template.spellcategorycooldown_5 = ReadInt32();

            template.bonding = (byte)ReadUInt32();
            template.description = ReadString();
            template.pagetext = ReadUInt32();
            template.languageid = (byte)ReadUInt32();
            template.pagematerial = (byte)ReadUInt32();
            template.startquest = ReadUInt32();
            template.lockid = ReadUInt32();
            template.material = (sbyte)ReadUInt32();
            template.sheath = (byte)ReadUInt32();
            template.randomproperty = ReadUInt32();
            template.randomsuffix = ReadUInt32();
            template.block = ReadUInt32();
            template.itemset = ReadUInt32();
            template.maxdurability = (ushort)ReadUInt32();
            template.area = ReadUInt32();
            template.map = (short)ReadUInt32();
            template.bagfamily = (int)ReadUInt32();
            template.totemcategory = (int)ReadUInt32();

            template.socketcolor_1 = (sbyte)ReadUInt32();
            template.socketcontent_1 = (int)ReadUInt32();

            template.socketcolor_2 = (sbyte)ReadUInt32();
            template.socketcontent_2 = (int)ReadUInt32();

            template.socketcolor_3 = (sbyte)ReadUInt32();
            template.socketcontent_3 = (int)ReadUInt32();

            template.socketbonus = (int)ReadUInt32();

            if (ClientBuild >= 7561)
            {
                template.gemproperties = (int)ReadUInt32();
            }

            if (ClientBuild <= 7799)
            {
                ReadUInt32(); //ExtendedCost
                ReadUInt32(); //RequiredArenaRank
            }

            template.requireddisenchantskill = (short)ReadUInt32();
            template.armordamagemodifier = ReadSingle();

            if (ClientBuild >= 8209)
            {
                template.duration = ReadUInt32();// added in 2.4.2.8209, duration (seconds)
            }

            if (ClientBuild >= 9183)
            {
                template.itemlimitcategory = (short)ReadUInt32();
            }

            if (ClientBuild > 9551)
            {
                template.holidayid = ReadUInt32();
            }


            return template;
        }
    }
}
